{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from model.db import DB_ENGINE\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "import logging\n",
    "import jieba\n",
    "import jieba.analyse\n",
    "from math import sqrt\n",
    "import os\n",
    "from pprint import pprint"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "logging.basicConfig(level=logging.INFO, format='%(asctime)s %(message)s')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>content</th>\n",
       "      <th>tag</th>\n",
       "      <th>assure</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>rid</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>比特币现价41000元左右。至今年底最少跌去一半！立此帖为证。</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>破5000是大概率事件</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>估计到时候都是非去中心化的币才是追捧的对象。没有信用背书的币还是不太靠谱。</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>出天涯钻，5毛一个</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td></td>\n",
       "      <td>1.0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                   content  tag  assure\n",
       "rid                                                    \n",
       "1          比特币现价41000元左右。至今年底最少跌去一半！立此帖为证。  1.0       1\n",
       "2                              破5000是大概率事件  1.0       1\n",
       "3    估计到时候都是非去中心化的币才是追捧的对象。没有信用背书的币还是不太靠谱。  0.0       1\n",
       "4                                出天涯钻，5毛一个  1.0       1\n",
       "5                                           1.0       1"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "raw_contents = pd.read_sql('SELECT rid, content, tag, assure FROM rawcontents', DB_ENGINE, index_col='rid')\n",
    "raw_contents.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "              tag  assure\n",
      "count  2025.00000  2025.0\n",
      "mean      0.28642     1.0\n",
      "std       0.45220     0.0\n",
      "min       0.00000     1.0\n",
      "25%       0.00000     1.0\n",
      "50%       0.00000     1.0\n",
      "75%       1.00000     1.0\n",
      "max       1.00000     1.0\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>content</th>\n",
       "      <th>tag</th>\n",
       "      <th>assure</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>rid</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>比特币现价41000元左右。至今年底最少跌去一半！立此帖为证。</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>破5000是大概率事件</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>估计到时候都是非去中心化的币才是追捧的对象。没有信用背书的币还是不太靠谱。</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>出天涯钻，5毛一个</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td></td>\n",
       "      <td>1.0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                   content  tag  assure\n",
       "rid                                                    \n",
       "1          比特币现价41000元左右。至今年底最少跌去一半！立此帖为证。  1.0       1\n",
       "2                              破5000是大概率事件  1.0       1\n",
       "3    估计到时候都是非去中心化的币才是追捧的对象。没有信用背书的币还是不太靠谱。  0.0       1\n",
       "4                                出天涯钻，5毛一个  1.0       1\n",
       "5                                           1.0       1"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tagged_data = raw_contents[raw_contents['assure'] > 0].copy()\n",
    "print(tagged_data.describe())\n",
    "tagged_data.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Fit"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.pipeline import Pipeline\n",
    "from sklearn.feature_extraction.text import CountVectorizer\n",
    "from sklearn.feature_extraction.text import TfidfTransformer\n",
    "from sklearn.linear_model import SGDClassifier"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "text_clf = Pipeline([\n",
    "    ('vect', CountVectorizer()),\n",
    "    ('tfidf', TfidfTransformer()),\n",
    "    ('clf', SGDClassifier(random_state=42, max_iter=64 ,tol=None)) # sublinear_tf \n",
    "])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "scale = 0.6\n",
    "mask = np.random.random((len(tagged_data)))\n",
    "train_data = tagged_data[mask <= scale]\n",
    "test_data = tagged_data[mask > scale]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/home/lx/.local/lib/python3.6/site-packages/sklearn/linear_model/stochastic_gradient.py:183: FutureWarning: max_iter and tol parameters have been added in SGDClassifier in 0.19. If max_iter is set but tol is left unset, the default value for tol in 0.19 and 0.20 will be None (which is equivalent to -infinity, so it has no effect) but will change in 0.21 to 1e-3. Specify tol to silence this warning.\n",
      "  FutureWarning)\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "Pipeline(memory=None,\n",
       "     steps=[('vect', CountVectorizer(analyzer='word', binary=False, decode_error='strict',\n",
       "        dtype=<class 'numpy.int64'>, encoding='utf-8', input='content',\n",
       "        lowercase=True, max_df=1.0, max_features=None, min_df=1,\n",
       "        ngram_range=(1, 1), preprocessor=None, stop_words=None,\n",
       "        strip...dom_state=42, shuffle=True, tol=None,\n",
       "       validation_fraction=0.1, verbose=0, warm_start=False))])"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "text_clf.fit(train_data['content'].values, train_data['tag'].values)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.6964705882352941"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "predicted = text_clf.predict(test_data['content'].values)\n",
    "np.mean(predicted == test_data['tag'].values)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Grid Search"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.model_selection import GridSearchCV\n",
    "from sklearn.model_selection import RandomizedSearchCV"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {},
   "outputs": [],
   "source": [
    "def tok2(sentence):\n",
    "    return jieba.lcut(sentence)\n",
    "\n",
    "\n",
    "text_clf = Pipeline([\n",
    "    ('vect', CountVectorizer(tokenizer=tok2)),\n",
    "    ('tfidf', TfidfTransformer()),\n",
    "    ('clf', SGDClassifier(random_state=ord(os.urandom(1)), max_iter=512, tol=1e-3)) # sublinear_tf \n",
    "])\n",
    "\n",
    "record = []\n",
    "\n",
    "def Search(parameters):\n",
    "    for i in range(10):\n",
    "        gs_clf =  RandomizedSearchCV(text_clf, parameters, n_jobs=-1, cv=5, iid=False)\n",
    "        gs_clf_result = gs_clf.fit(train_data['content'].values, train_data['tag'].values)\n",
    "        print(i, gs_clf_result.best_score_)\n",
    "        record.append( dict([('score', gs_clf_result.best_score_), *gs_clf_result.best_params_.items()]) )\n",
    "    \n",
    "    df = pd.DataFrame(record)\n",
    "    return df.sort_values('score', ascending=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.7395979238135746 {'vect__ngram_range': (1, 2), 'tfidf__use_idf': False, 'tfidf__sublinear_tf': False, 'tfidf__smooth_idf': True, 'tfidf__norm': 'l1', 'clf__penalty': 'l2', 'clf__loss': 'squared_hinge', 'clf__fit_intercept': False, 'clf__alpha': 1}\n",
      "0.7370230026414664 {'vect__ngram_range': (1, 2), 'tfidf__use_idf': True, 'tfidf__sublinear_tf': True, 'tfidf__smooth_idf': True, 'tfidf__norm': None, 'clf__penalty': 'elasticnet', 'clf__loss': 'hinge', 'clf__fit_intercept': False, 'clf__alpha': 1}\n",
      "0.737044701501968 {'vect__ngram_range': (1, 3), 'tfidf__use_idf': True, 'tfidf__sublinear_tf': True, 'tfidf__smooth_idf': True, 'tfidf__norm': 'l1', 'clf__penalty': 'l2', 'clf__loss': 'log', 'clf__fit_intercept': False, 'clf__alpha': 0.01}\n",
      "0.7370230026414664 {'vect__ngram_range': (1, 1), 'tfidf__use_idf': False, 'tfidf__sublinear_tf': True, 'tfidf__smooth_idf': False, 'tfidf__norm': 'l1', 'clf__penalty': 'l1', 'clf__loss': 'modified_huber', 'clf__fit_intercept': False, 'clf__alpha': 1}\n",
      "0.737026608844135 {'vect__ngram_range': (1, 3), 'tfidf__use_idf': True, 'tfidf__sublinear_tf': False, 'tfidf__smooth_idf': False, 'tfidf__norm': 'l2', 'clf__penalty': 'l2', 'clf__loss': 'modified_huber', 'clf__fit_intercept': True, 'clf__alpha': 0.01}\n",
      "0.7370230026414664 {'vect__ngram_range': (1, 2), 'tfidf__use_idf': False, 'tfidf__sublinear_tf': False, 'tfidf__smooth_idf': False, 'tfidf__norm': 'l1', 'clf__penalty': 'l1', 'clf__loss': 'modified_huber', 'clf__fit_intercept': True, 'clf__alpha': 0.1}\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/home/lx/.local/lib/python3.6/site-packages/sklearn/linear_model/stochastic_gradient.py:603: ConvergenceWarning: Maximum number of iteration reached before convergence. Consider increasing max_iter to improve the fit.\n",
      "  ConvergenceWarning)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.7438751267564827 {'vect__ngram_range': (1, 3), 'tfidf__use_idf': True, 'tfidf__sublinear_tf': False, 'tfidf__smooth_idf': False, 'tfidf__norm': None, 'clf__penalty': 'l1', 'clf__loss': 'perceptron', 'clf__fit_intercept': False, 'clf__alpha': 0.1}\n",
      "0.7395979238135746 {'vect__ngram_range': (1, 2), 'tfidf__use_idf': False, 'tfidf__sublinear_tf': False, 'tfidf__smooth_idf': True, 'tfidf__norm': 'l1', 'clf__penalty': 'l2', 'clf__loss': 'squared_hinge', 'clf__fit_intercept': False, 'clf__alpha': 0.1}\n",
      "0.7378957653317553 {'vect__ngram_range': (1, 3), 'tfidf__use_idf': True, 'tfidf__sublinear_tf': True, 'tfidf__smooth_idf': True, 'tfidf__norm': 'l1', 'clf__penalty': 'l2', 'clf__loss': 'modified_huber', 'clf__fit_intercept': False, 'clf__alpha': 0.01}\n",
      "0.7370230026414664 {'vect__ngram_range': (1, 1), 'tfidf__use_idf': False, 'tfidf__sublinear_tf': True, 'tfidf__smooth_idf': True, 'tfidf__norm': 'l2', 'clf__penalty': 'elasticnet', 'clf__loss': 'modified_huber', 'clf__fit_intercept': False, 'clf__alpha': 0.1}\n"
     ]
    }
   ],
   "source": [
    "parameters = {\n",
    "    'vect__ngram_range': [(1, 1), (1, 2), (1, 3)],\n",
    "    'tfidf__norm': (None, 'l2', 'l1'),\n",
    "    'tfidf__use_idf': (True, False),\n",
    "    'tfidf__sublinear_tf': (True, False),\n",
    "    'tfidf__smooth_idf': (True, False),\n",
    "    'clf__penalty': ('none', 'l2', 'l1', 'elasticnet'),\n",
    "    'clf__loss': ('hinge', 'log', 'modified_huber', 'squared_hinge', 'perceptron'),\n",
    "    'clf__fit_intercept': (True, False),\n",
    "    'clf__alpha': (1, 1e-1, 1e-2, 1e-3)\n",
    "}\n",
    "\n",
    "Search(parameters).head(10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.7370230026414664\n",
      "0.7370230026414664\n",
      "0.7370230026414664\n",
      "0.7370230026414664\n",
      "0.7404960223892788\n",
      "0.7370302458690485\n",
      "0.7395979238135746\n",
      "0.7370266396663799\n",
      "0.7370230026414664\n",
      "0.7395979238135746\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>clf__alpha</th>\n",
       "      <th>clf__fit_intercept</th>\n",
       "      <th>clf__loss</th>\n",
       "      <th>clf__penalty</th>\n",
       "      <th>score</th>\n",
       "      <th>tfidf__norm</th>\n",
       "      <th>tfidf__smooth_idf</th>\n",
       "      <th>tfidf__sublinear_tf</th>\n",
       "      <th>tfidf__use_idf</th>\n",
       "      <th>vect__ngram_range</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>1.000</td>\n",
       "      <td>True</td>\n",
       "      <td>modified_huber</td>\n",
       "      <td>elasticnet</td>\n",
       "      <td>0.752386</td>\n",
       "      <td>None</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>(1, 3)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25</th>\n",
       "      <td>1.000</td>\n",
       "      <td>False</td>\n",
       "      <td>modified_huber</td>\n",
       "      <td>elasticnet</td>\n",
       "      <td>0.750727</td>\n",
       "      <td>None</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>(1, 3)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>16</th>\n",
       "      <td>0.001</td>\n",
       "      <td>False</td>\n",
       "      <td>perceptron</td>\n",
       "      <td>l1</td>\n",
       "      <td>0.744715</td>\n",
       "      <td>l2</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>(1, 2)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>29</th>\n",
       "      <td>2.000</td>\n",
       "      <td>True</td>\n",
       "      <td>modified_huber</td>\n",
       "      <td>elasticnet</td>\n",
       "      <td>0.744708</td>\n",
       "      <td>None</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>(1, 2)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>0.100</td>\n",
       "      <td>False</td>\n",
       "      <td>modified_huber</td>\n",
       "      <td>elasticnet</td>\n",
       "      <td>0.743958</td>\n",
       "      <td>None</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>(1, 3)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>0.010</td>\n",
       "      <td>False</td>\n",
       "      <td>perceptron</td>\n",
       "      <td>elasticnet</td>\n",
       "      <td>0.742129</td>\n",
       "      <td>l2</td>\n",
       "      <td>True</td>\n",
       "      <td>True</td>\n",
       "      <td>True</td>\n",
       "      <td>(1, 2)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>34</th>\n",
       "      <td>0.100</td>\n",
       "      <td>True</td>\n",
       "      <td>modified_huber</td>\n",
       "      <td>l2</td>\n",
       "      <td>0.740496</td>\n",
       "      <td>None</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>(1, 2)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>0.001</td>\n",
       "      <td>False</td>\n",
       "      <td>perceptron</td>\n",
       "      <td>elasticnet</td>\n",
       "      <td>0.740485</td>\n",
       "      <td>l1</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>(1, 4)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>26</th>\n",
       "      <td>2.000</td>\n",
       "      <td>False</td>\n",
       "      <td>modified_huber</td>\n",
       "      <td>elasticnet</td>\n",
       "      <td>0.740456</td>\n",
       "      <td>None</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>(1, 4)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>36</th>\n",
       "      <td>2.000</td>\n",
       "      <td>False</td>\n",
       "      <td>squared_hinge</td>\n",
       "      <td>l2</td>\n",
       "      <td>0.739598</td>\n",
       "      <td>l1</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>(1, 4)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0.100</td>\n",
       "      <td>False</td>\n",
       "      <td>modified_huber</td>\n",
       "      <td>l2</td>\n",
       "      <td>0.739598</td>\n",
       "      <td>l1</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>(1, 3)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>39</th>\n",
       "      <td>1.000</td>\n",
       "      <td>False</td>\n",
       "      <td>log</td>\n",
       "      <td>l2</td>\n",
       "      <td>0.739598</td>\n",
       "      <td>l1</td>\n",
       "      <td>True</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>(1, 3)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>0.100</td>\n",
       "      <td>False</td>\n",
       "      <td>perceptron</td>\n",
       "      <td>elasticnet</td>\n",
       "      <td>0.739587</td>\n",
       "      <td>None</td>\n",
       "      <td>True</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>(1, 3)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>27</th>\n",
       "      <td>0.010</td>\n",
       "      <td>False</td>\n",
       "      <td>perceptron</td>\n",
       "      <td>l1</td>\n",
       "      <td>0.738761</td>\n",
       "      <td>None</td>\n",
       "      <td>True</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>(1, 2)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>0.100</td>\n",
       "      <td>False</td>\n",
       "      <td>modified_huber</td>\n",
       "      <td>l2</td>\n",
       "      <td>0.738747</td>\n",
       "      <td>l1</td>\n",
       "      <td>True</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>(1, 3)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>0.010</td>\n",
       "      <td>False</td>\n",
       "      <td>modified_huber</td>\n",
       "      <td>l1</td>\n",
       "      <td>0.737874</td>\n",
       "      <td>l1</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>(1, 3)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>0.010</td>\n",
       "      <td>False</td>\n",
       "      <td>modified_huber</td>\n",
       "      <td>l1</td>\n",
       "      <td>0.737874</td>\n",
       "      <td>l1</td>\n",
       "      <td>True</td>\n",
       "      <td>True</td>\n",
       "      <td>True</td>\n",
       "      <td>(1, 3)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0.001</td>\n",
       "      <td>True</td>\n",
       "      <td>squared_hinge</td>\n",
       "      <td>l1</td>\n",
       "      <td>0.737157</td>\n",
       "      <td>l2</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>True</td>\n",
       "      <td>(1, 3)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>0.100</td>\n",
       "      <td>False</td>\n",
       "      <td>hinge</td>\n",
       "      <td>none</td>\n",
       "      <td>0.737045</td>\n",
       "      <td>l1</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>(1, 4)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>35</th>\n",
       "      <td>1.000</td>\n",
       "      <td>True</td>\n",
       "      <td>log</td>\n",
       "      <td>elasticnet</td>\n",
       "      <td>0.737030</td>\n",
       "      <td>None</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>(1, 2)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>37</th>\n",
       "      <td>0.100</td>\n",
       "      <td>True</td>\n",
       "      <td>log</td>\n",
       "      <td>elasticnet</td>\n",
       "      <td>0.737027</td>\n",
       "      <td>None</td>\n",
       "      <td>True</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>(1, 3)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>18</th>\n",
       "      <td>0.100</td>\n",
       "      <td>True</td>\n",
       "      <td>modified_huber</td>\n",
       "      <td>elasticnet</td>\n",
       "      <td>0.737023</td>\n",
       "      <td>l1</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>(1, 4)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>19</th>\n",
       "      <td>1.000</td>\n",
       "      <td>False</td>\n",
       "      <td>modified_huber</td>\n",
       "      <td>l1</td>\n",
       "      <td>0.737023</td>\n",
       "      <td>l2</td>\n",
       "      <td>True</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>(1, 4)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>38</th>\n",
       "      <td>1.000</td>\n",
       "      <td>False</td>\n",
       "      <td>log</td>\n",
       "      <td>l1</td>\n",
       "      <td>0.737023</td>\n",
       "      <td>None</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>(1, 2)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>0.100</td>\n",
       "      <td>False</td>\n",
       "      <td>perceptron</td>\n",
       "      <td>l1</td>\n",
       "      <td>0.737023</td>\n",
       "      <td>l1</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>(1, 3)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>33</th>\n",
       "      <td>0.100</td>\n",
       "      <td>False</td>\n",
       "      <td>perceptron</td>\n",
       "      <td>l1</td>\n",
       "      <td>0.737023</td>\n",
       "      <td>l1</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>(1, 4)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>32</th>\n",
       "      <td>2.000</td>\n",
       "      <td>True</td>\n",
       "      <td>log</td>\n",
       "      <td>elasticnet</td>\n",
       "      <td>0.737023</td>\n",
       "      <td>None</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>(1, 3)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>31</th>\n",
       "      <td>0.010</td>\n",
       "      <td>False</td>\n",
       "      <td>log</td>\n",
       "      <td>elasticnet</td>\n",
       "      <td>0.737023</td>\n",
       "      <td>l1</td>\n",
       "      <td>True</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>(1, 2)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>30</th>\n",
       "      <td>0.010</td>\n",
       "      <td>True</td>\n",
       "      <td>modified_huber</td>\n",
       "      <td>elasticnet</td>\n",
       "      <td>0.737023</td>\n",
       "      <td>l1</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>True</td>\n",
       "      <td>(1, 2)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>0.100</td>\n",
       "      <td>True</td>\n",
       "      <td>hinge</td>\n",
       "      <td>l1</td>\n",
       "      <td>0.737023</td>\n",
       "      <td>l1</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>True</td>\n",
       "      <td>(1, 2)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>28</th>\n",
       "      <td>0.100</td>\n",
       "      <td>True</td>\n",
       "      <td>modified_huber</td>\n",
       "      <td>elasticnet</td>\n",
       "      <td>0.737023</td>\n",
       "      <td>l1</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>(1, 2)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>1.000</td>\n",
       "      <td>True</td>\n",
       "      <td>modified_huber</td>\n",
       "      <td>l1</td>\n",
       "      <td>0.737023</td>\n",
       "      <td>None</td>\n",
       "      <td>True</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>(1, 5)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>1.000</td>\n",
       "      <td>True</td>\n",
       "      <td>modified_huber</td>\n",
       "      <td>l1</td>\n",
       "      <td>0.737023</td>\n",
       "      <td>l2</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>(1, 3)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15</th>\n",
       "      <td>0.010</td>\n",
       "      <td>False</td>\n",
       "      <td>perceptron</td>\n",
       "      <td>l1</td>\n",
       "      <td>0.737023</td>\n",
       "      <td>l1</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>(1, 5)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>24</th>\n",
       "      <td>1.000</td>\n",
       "      <td>True</td>\n",
       "      <td>perceptron</td>\n",
       "      <td>l1</td>\n",
       "      <td>0.737023</td>\n",
       "      <td>None</td>\n",
       "      <td>True</td>\n",
       "      <td>True</td>\n",
       "      <td>True</td>\n",
       "      <td>(1, 4)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23</th>\n",
       "      <td>2.000</td>\n",
       "      <td>False</td>\n",
       "      <td>perceptron</td>\n",
       "      <td>elasticnet</td>\n",
       "      <td>0.737023</td>\n",
       "      <td>l2</td>\n",
       "      <td>True</td>\n",
       "      <td>True</td>\n",
       "      <td>True</td>\n",
       "      <td>(1, 2)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>22</th>\n",
       "      <td>0.100</td>\n",
       "      <td>False</td>\n",
       "      <td>modified_huber</td>\n",
       "      <td>l1</td>\n",
       "      <td>0.737023</td>\n",
       "      <td>l2</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>(1, 2)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>21</th>\n",
       "      <td>1.000</td>\n",
       "      <td>True</td>\n",
       "      <td>modified_huber</td>\n",
       "      <td>l1</td>\n",
       "      <td>0.737023</td>\n",
       "      <td>l1</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>(1, 3)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17</th>\n",
       "      <td>1.000</td>\n",
       "      <td>True</td>\n",
       "      <td>modified_huber</td>\n",
       "      <td>l1</td>\n",
       "      <td>0.737023</td>\n",
       "      <td>None</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>(1, 2)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20</th>\n",
       "      <td>0.100</td>\n",
       "      <td>False</td>\n",
       "      <td>perceptron</td>\n",
       "      <td>l1</td>\n",
       "      <td>0.737023</td>\n",
       "      <td>l2</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>(1, 3)</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    clf__alpha  clf__fit_intercept       clf__loss clf__penalty     score  \\\n",
       "12       1.000                True  modified_huber   elasticnet  0.752386   \n",
       "25       1.000               False  modified_huber   elasticnet  0.750727   \n",
       "16       0.001               False      perceptron           l1  0.744715   \n",
       "29       2.000                True  modified_huber   elasticnet  0.744708   \n",
       "2        0.100               False  modified_huber   elasticnet  0.743958   \n",
       "5        0.010               False      perceptron   elasticnet  0.742129   \n",
       "34       0.100                True  modified_huber           l2  0.740496   \n",
       "3        0.001               False      perceptron   elasticnet  0.740485   \n",
       "26       2.000               False  modified_huber   elasticnet  0.740456   \n",
       "36       2.000               False   squared_hinge           l2  0.739598   \n",
       "1        0.100               False  modified_huber           l2  0.739598   \n",
       "39       1.000               False             log           l2  0.739598   \n",
       "13       0.100               False      perceptron   elasticnet  0.739587   \n",
       "27       0.010               False      perceptron           l1  0.738761   \n",
       "6        0.100               False  modified_huber           l2  0.738747   \n",
       "10       0.010               False  modified_huber           l1  0.737874   \n",
       "11       0.010               False  modified_huber           l1  0.737874   \n",
       "0        0.001                True   squared_hinge           l1  0.737157   \n",
       "4        0.100               False           hinge         none  0.737045   \n",
       "35       1.000                True             log   elasticnet  0.737030   \n",
       "37       0.100                True             log   elasticnet  0.737027   \n",
       "18       0.100                True  modified_huber   elasticnet  0.737023   \n",
       "19       1.000               False  modified_huber           l1  0.737023   \n",
       "38       1.000               False             log           l1  0.737023   \n",
       "7        0.100               False      perceptron           l1  0.737023   \n",
       "33       0.100               False      perceptron           l1  0.737023   \n",
       "32       2.000                True             log   elasticnet  0.737023   \n",
       "31       0.010               False             log   elasticnet  0.737023   \n",
       "30       0.010                True  modified_huber   elasticnet  0.737023   \n",
       "8        0.100                True           hinge           l1  0.737023   \n",
       "28       0.100                True  modified_huber   elasticnet  0.737023   \n",
       "9        1.000                True  modified_huber           l1  0.737023   \n",
       "14       1.000                True  modified_huber           l1  0.737023   \n",
       "15       0.010               False      perceptron           l1  0.737023   \n",
       "24       1.000                True      perceptron           l1  0.737023   \n",
       "23       2.000               False      perceptron   elasticnet  0.737023   \n",
       "22       0.100               False  modified_huber           l1  0.737023   \n",
       "21       1.000                True  modified_huber           l1  0.737023   \n",
       "17       1.000                True  modified_huber           l1  0.737023   \n",
       "20       0.100               False      perceptron           l1  0.737023   \n",
       "\n",
       "   tfidf__norm  tfidf__smooth_idf  tfidf__sublinear_tf  tfidf__use_idf  \\\n",
       "12        None              False                False            True   \n",
       "25        None              False                False            True   \n",
       "16          l2               True                False            True   \n",
       "29        None               True                False            True   \n",
       "2         None              False                False            True   \n",
       "5           l2               True                 True            True   \n",
       "34        None               True                False           False   \n",
       "3           l1               True                False           False   \n",
       "26        None               True                False            True   \n",
       "36          l1              False                False           False   \n",
       "1           l1               True                False           False   \n",
       "39          l1               True                 True           False   \n",
       "13        None               True                 True           False   \n",
       "27        None               True                 True           False   \n",
       "6           l1               True                 True           False   \n",
       "10          l1               True                False            True   \n",
       "11          l1               True                 True            True   \n",
       "0           l2              False                 True            True   \n",
       "4           l1               True                False           False   \n",
       "35        None               True                False            True   \n",
       "37        None               True                 True           False   \n",
       "18          l1               True                False           False   \n",
       "19          l2               True                 True           False   \n",
       "38        None              False                False            True   \n",
       "7           l1               True                False           False   \n",
       "33          l1              False                False           False   \n",
       "32        None               True                False            True   \n",
       "31          l1               True                 True           False   \n",
       "30          l1              False                 True            True   \n",
       "8           l1              False                 True            True   \n",
       "28          l1               True                False           False   \n",
       "9         None               True                 True           False   \n",
       "14          l2              False                False           False   \n",
       "15          l1              False                 True           False   \n",
       "24        None               True                 True            True   \n",
       "23          l2               True                 True            True   \n",
       "22          l2              False                False            True   \n",
       "21          l1              False                 True           False   \n",
       "17        None              False                 True           False   \n",
       "20          l2               True                False           False   \n",
       "\n",
       "   vect__ngram_range  \n",
       "12            (1, 3)  \n",
       "25            (1, 3)  \n",
       "16            (1, 2)  \n",
       "29            (1, 2)  \n",
       "2             (1, 3)  \n",
       "5             (1, 2)  \n",
       "34            (1, 2)  \n",
       "3             (1, 4)  \n",
       "26            (1, 4)  \n",
       "36            (1, 4)  \n",
       "1             (1, 3)  \n",
       "39            (1, 3)  \n",
       "13            (1, 3)  \n",
       "27            (1, 2)  \n",
       "6             (1, 3)  \n",
       "10            (1, 3)  \n",
       "11            (1, 3)  \n",
       "0             (1, 3)  \n",
       "4             (1, 4)  \n",
       "35            (1, 2)  \n",
       "37            (1, 3)  \n",
       "18            (1, 4)  \n",
       "19            (1, 4)  \n",
       "38            (1, 2)  \n",
       "7             (1, 3)  \n",
       "33            (1, 4)  \n",
       "32            (1, 3)  \n",
       "31            (1, 2)  \n",
       "30            (1, 2)  \n",
       "8             (1, 2)  \n",
       "28            (1, 2)  \n",
       "9             (1, 5)  \n",
       "14            (1, 3)  \n",
       "15            (1, 5)  \n",
       "24            (1, 4)  \n",
       "23            (1, 2)  \n",
       "22            (1, 2)  \n",
       "21            (1, 3)  \n",
       "17            (1, 2)  \n",
       "20            (1, 3)  "
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "parameters = {\n",
    "    'vect__ngram_range': [(1, 2), (1, 3), (1, 4)],#\n",
    "    'tfidf__norm': (None, 'l1'),#\n",
    "    'tfidf__use_idf': (True, False),\n",
    "    'tfidf__sublinear_tf': (True, False),\n",
    "    'tfidf__smooth_idf': (True, False),\n",
    "    'clf__penalty': ('l2', 'l1', 'elasticnet'),#\n",
    "    'clf__loss': ('log', 'modified_huber', 'squared_hinge', 'perceptron'),\n",
    "    'clf__fit_intercept': (True, False),\n",
    "    'clf__alpha': (2, 1, 1e-1, 1e-2, 1e-3)#\n",
    "}\n",
    "\n",
    "Search(parameters).head(10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.7371567403626546\n",
      "0.7395979238135746\n",
      "0.7439581618845953\n",
      "0.7404850496700479\n",
      "0.7370447323242131\n",
      "0.7421293856201898\n",
      "0.7387468599837875\n",
      "0.7370230026414664\n",
      "0.7370230026414664\n",
      "0.7370230026414664\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>clf__alpha</th>\n",
       "      <th>clf__fit_intercept</th>\n",
       "      <th>clf__loss</th>\n",
       "      <th>clf__penalty</th>\n",
       "      <th>score</th>\n",
       "      <th>tfidf__norm</th>\n",
       "      <th>tfidf__smooth_idf</th>\n",
       "      <th>tfidf__sublinear_tf</th>\n",
       "      <th>tfidf__use_idf</th>\n",
       "      <th>vect__ngram_range</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>0.100</td>\n",
       "      <td>False</td>\n",
       "      <td>modified_huber</td>\n",
       "      <td>elasticnet</td>\n",
       "      <td>0.743958</td>\n",
       "      <td>None</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>(1, 3)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>0.010</td>\n",
       "      <td>False</td>\n",
       "      <td>perceptron</td>\n",
       "      <td>elasticnet</td>\n",
       "      <td>0.742129</td>\n",
       "      <td>l2</td>\n",
       "      <td>True</td>\n",
       "      <td>True</td>\n",
       "      <td>True</td>\n",
       "      <td>(1, 2)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>0.001</td>\n",
       "      <td>False</td>\n",
       "      <td>perceptron</td>\n",
       "      <td>elasticnet</td>\n",
       "      <td>0.740485</td>\n",
       "      <td>l1</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>(1, 4)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0.100</td>\n",
       "      <td>False</td>\n",
       "      <td>modified_huber</td>\n",
       "      <td>l2</td>\n",
       "      <td>0.739598</td>\n",
       "      <td>l1</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>(1, 3)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>0.100</td>\n",
       "      <td>False</td>\n",
       "      <td>modified_huber</td>\n",
       "      <td>l2</td>\n",
       "      <td>0.738747</td>\n",
       "      <td>l1</td>\n",
       "      <td>True</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>(1, 3)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0.001</td>\n",
       "      <td>True</td>\n",
       "      <td>squared_hinge</td>\n",
       "      <td>l1</td>\n",
       "      <td>0.737157</td>\n",
       "      <td>l2</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>True</td>\n",
       "      <td>(1, 3)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>0.100</td>\n",
       "      <td>False</td>\n",
       "      <td>hinge</td>\n",
       "      <td>none</td>\n",
       "      <td>0.737045</td>\n",
       "      <td>l1</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>(1, 4)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>0.100</td>\n",
       "      <td>False</td>\n",
       "      <td>perceptron</td>\n",
       "      <td>l1</td>\n",
       "      <td>0.737023</td>\n",
       "      <td>l1</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>(1, 3)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>0.100</td>\n",
       "      <td>True</td>\n",
       "      <td>hinge</td>\n",
       "      <td>l1</td>\n",
       "      <td>0.737023</td>\n",
       "      <td>l1</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>True</td>\n",
       "      <td>(1, 2)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>1.000</td>\n",
       "      <td>True</td>\n",
       "      <td>modified_huber</td>\n",
       "      <td>l1</td>\n",
       "      <td>0.737023</td>\n",
       "      <td>None</td>\n",
       "      <td>True</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>(1, 5)</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   clf__alpha  clf__fit_intercept       clf__loss clf__penalty     score  \\\n",
       "2       0.100               False  modified_huber   elasticnet  0.743958   \n",
       "5       0.010               False      perceptron   elasticnet  0.742129   \n",
       "3       0.001               False      perceptron   elasticnet  0.740485   \n",
       "1       0.100               False  modified_huber           l2  0.739598   \n",
       "6       0.100               False  modified_huber           l2  0.738747   \n",
       "0       0.001                True   squared_hinge           l1  0.737157   \n",
       "4       0.100               False           hinge         none  0.737045   \n",
       "7       0.100               False      perceptron           l1  0.737023   \n",
       "8       0.100                True           hinge           l1  0.737023   \n",
       "9       1.000                True  modified_huber           l1  0.737023   \n",
       "\n",
       "  tfidf__norm  tfidf__smooth_idf  tfidf__sublinear_tf  tfidf__use_idf  \\\n",
       "2        None              False                False            True   \n",
       "5          l2               True                 True            True   \n",
       "3          l1               True                False           False   \n",
       "1          l1               True                False           False   \n",
       "6          l1               True                 True           False   \n",
       "0          l2              False                 True            True   \n",
       "4          l1               True                False           False   \n",
       "7          l1               True                False           False   \n",
       "8          l1              False                 True            True   \n",
       "9        None               True                 True           False   \n",
       "\n",
       "  vect__ngram_range  \n",
       "2            (1, 3)  \n",
       "5            (1, 2)  \n",
       "3            (1, 4)  \n",
       "1            (1, 3)  \n",
       "6            (1, 3)  \n",
       "0            (1, 3)  \n",
       "4            (1, 4)  \n",
       "7            (1, 3)  \n",
       "8            (1, 2)  \n",
       "9            (1, 5)  "
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "parameters = {\n",
    "    'vect__ngram_range': [(1, 2), (1, 3), (1, 4), (1, 5)], #\n",
    "    'tfidf__norm': (None, 'l2', 'l1'),\n",
    "    'tfidf__use_idf': (True, False),\n",
    "    'tfidf__sublinear_tf': (True, False),\n",
    "    'tfidf__smooth_idf': (True, False),\n",
    "    'clf__penalty': ('none', 'l2', 'l1', 'elasticnet'),\n",
    "    'clf__loss': ('hinge', 'log', 'modified_huber', 'squared_hinge', 'perceptron'),\n",
    "    'clf__fit_intercept': (True, False),\n",
    "    'clf__alpha': (1, 1e-1, 1e-2, 1e-3, 1e-4)#\n",
    "}\n",
    "\n",
    "Search(parameters).head(10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0 0.7378740664712536\n",
      "1 0.7378740664712536\n",
      "2 0.752385703409865\n",
      "3 0.7395871052055689\n",
      "4 0.7370230026414664\n",
      "5 0.7370230026414664\n",
      "6 0.7447151254003039\n",
      "7 0.7370230026414664\n",
      "8 0.7370230026414664\n",
      "9 0.7370230026414664\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>clf__alpha</th>\n",
       "      <th>clf__fit_intercept</th>\n",
       "      <th>clf__loss</th>\n",
       "      <th>clf__penalty</th>\n",
       "      <th>score</th>\n",
       "      <th>tfidf__norm</th>\n",
       "      <th>tfidf__smooth_idf</th>\n",
       "      <th>tfidf__sublinear_tf</th>\n",
       "      <th>tfidf__use_idf</th>\n",
       "      <th>vect__ngram_range</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>1.000</td>\n",
       "      <td>True</td>\n",
       "      <td>modified_huber</td>\n",
       "      <td>elasticnet</td>\n",
       "      <td>0.752386</td>\n",
       "      <td>None</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>(1, 3)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>16</th>\n",
       "      <td>0.001</td>\n",
       "      <td>False</td>\n",
       "      <td>perceptron</td>\n",
       "      <td>l1</td>\n",
       "      <td>0.744715</td>\n",
       "      <td>l2</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>(1, 2)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>0.100</td>\n",
       "      <td>False</td>\n",
       "      <td>modified_huber</td>\n",
       "      <td>elasticnet</td>\n",
       "      <td>0.743958</td>\n",
       "      <td>None</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>(1, 3)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>0.010</td>\n",
       "      <td>False</td>\n",
       "      <td>perceptron</td>\n",
       "      <td>elasticnet</td>\n",
       "      <td>0.742129</td>\n",
       "      <td>l2</td>\n",
       "      <td>True</td>\n",
       "      <td>True</td>\n",
       "      <td>True</td>\n",
       "      <td>(1, 2)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>0.001</td>\n",
       "      <td>False</td>\n",
       "      <td>perceptron</td>\n",
       "      <td>elasticnet</td>\n",
       "      <td>0.740485</td>\n",
       "      <td>l1</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>(1, 4)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0.100</td>\n",
       "      <td>False</td>\n",
       "      <td>modified_huber</td>\n",
       "      <td>l2</td>\n",
       "      <td>0.739598</td>\n",
       "      <td>l1</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>(1, 3)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>0.100</td>\n",
       "      <td>False</td>\n",
       "      <td>perceptron</td>\n",
       "      <td>elasticnet</td>\n",
       "      <td>0.739587</td>\n",
       "      <td>None</td>\n",
       "      <td>True</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>(1, 3)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>0.100</td>\n",
       "      <td>False</td>\n",
       "      <td>modified_huber</td>\n",
       "      <td>l2</td>\n",
       "      <td>0.738747</td>\n",
       "      <td>l1</td>\n",
       "      <td>True</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>(1, 3)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>0.010</td>\n",
       "      <td>False</td>\n",
       "      <td>modified_huber</td>\n",
       "      <td>l1</td>\n",
       "      <td>0.737874</td>\n",
       "      <td>l1</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>(1, 3)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>0.010</td>\n",
       "      <td>False</td>\n",
       "      <td>modified_huber</td>\n",
       "      <td>l1</td>\n",
       "      <td>0.737874</td>\n",
       "      <td>l1</td>\n",
       "      <td>True</td>\n",
       "      <td>True</td>\n",
       "      <td>True</td>\n",
       "      <td>(1, 3)</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    clf__alpha  clf__fit_intercept       clf__loss clf__penalty     score  \\\n",
       "12       1.000                True  modified_huber   elasticnet  0.752386   \n",
       "16       0.001               False      perceptron           l1  0.744715   \n",
       "2        0.100               False  modified_huber   elasticnet  0.743958   \n",
       "5        0.010               False      perceptron   elasticnet  0.742129   \n",
       "3        0.001               False      perceptron   elasticnet  0.740485   \n",
       "1        0.100               False  modified_huber           l2  0.739598   \n",
       "13       0.100               False      perceptron   elasticnet  0.739587   \n",
       "6        0.100               False  modified_huber           l2  0.738747   \n",
       "10       0.010               False  modified_huber           l1  0.737874   \n",
       "11       0.010               False  modified_huber           l1  0.737874   \n",
       "\n",
       "   tfidf__norm  tfidf__smooth_idf  tfidf__sublinear_tf  tfidf__use_idf  \\\n",
       "12        None              False                False            True   \n",
       "16          l2               True                False            True   \n",
       "2         None              False                False            True   \n",
       "5           l2               True                 True            True   \n",
       "3           l1               True                False           False   \n",
       "1           l1               True                False           False   \n",
       "13        None               True                 True           False   \n",
       "6           l1               True                 True           False   \n",
       "10          l1               True                False            True   \n",
       "11          l1               True                 True            True   \n",
       "\n",
       "   vect__ngram_range  \n",
       "12            (1, 3)  \n",
       "16            (1, 2)  \n",
       "2             (1, 3)  \n",
       "5             (1, 2)  \n",
       "3             (1, 4)  \n",
       "1             (1, 3)  \n",
       "13            (1, 3)  \n",
       "6             (1, 3)  \n",
       "10            (1, 3)  \n",
       "11            (1, 3)  "
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "parameters = {\n",
    "    'vect__ngram_range': [(1, 2), (1, 3), (1, 4), (1, 5)],\n",
    "    'tfidf__norm': (None, 'l2', 'l1'),\n",
    "    'tfidf__use_idf': [True, False],\n",
    "    'tfidf__sublinear_tf': [True, False],\n",
    "    'tfidf__smooth_idf': [True, False],\n",
    "    'clf__penalty': ('l1', 'elasticnet'), #\n",
    "    'clf__loss': ('modified_huber', 'perceptron'), #\n",
    "    'clf__fit_intercept': [True, False],\n",
    "    'clf__alpha': (1, 1e-1, 1e-2, 1e-3, 1e-4)#\n",
    "}\n",
    "\n",
    "Search(parameters).head(10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0 0.7370230026414664\n",
      "1 0.7370230026414664\n",
      "2 0.7370230026414664\n",
      "3 0.7370230026414664\n",
      "4 0.7370230026414664\n",
      "5 0.7507269734712937\n",
      "6 0.7404563233376793\n",
      "7 0.738761469727932\n",
      "8 0.7370230026414664\n",
      "9 0.7447080054617018\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>clf__alpha</th>\n",
       "      <th>clf__fit_intercept</th>\n",
       "      <th>clf__loss</th>\n",
       "      <th>clf__penalty</th>\n",
       "      <th>score</th>\n",
       "      <th>tfidf__norm</th>\n",
       "      <th>tfidf__smooth_idf</th>\n",
       "      <th>tfidf__sublinear_tf</th>\n",
       "      <th>tfidf__use_idf</th>\n",
       "      <th>vect__ngram_range</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>1.000</td>\n",
       "      <td>True</td>\n",
       "      <td>modified_huber</td>\n",
       "      <td>elasticnet</td>\n",
       "      <td>0.752386</td>\n",
       "      <td>None</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>(1, 3)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25</th>\n",
       "      <td>1.000</td>\n",
       "      <td>False</td>\n",
       "      <td>modified_huber</td>\n",
       "      <td>elasticnet</td>\n",
       "      <td>0.750727</td>\n",
       "      <td>None</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>(1, 3)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>16</th>\n",
       "      <td>0.001</td>\n",
       "      <td>False</td>\n",
       "      <td>perceptron</td>\n",
       "      <td>l1</td>\n",
       "      <td>0.744715</td>\n",
       "      <td>l2</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>(1, 2)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>29</th>\n",
       "      <td>2.000</td>\n",
       "      <td>True</td>\n",
       "      <td>modified_huber</td>\n",
       "      <td>elasticnet</td>\n",
       "      <td>0.744708</td>\n",
       "      <td>None</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>(1, 2)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>0.100</td>\n",
       "      <td>False</td>\n",
       "      <td>modified_huber</td>\n",
       "      <td>elasticnet</td>\n",
       "      <td>0.743958</td>\n",
       "      <td>None</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>(1, 3)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>0.010</td>\n",
       "      <td>False</td>\n",
       "      <td>perceptron</td>\n",
       "      <td>elasticnet</td>\n",
       "      <td>0.742129</td>\n",
       "      <td>l2</td>\n",
       "      <td>True</td>\n",
       "      <td>True</td>\n",
       "      <td>True</td>\n",
       "      <td>(1, 2)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>0.001</td>\n",
       "      <td>False</td>\n",
       "      <td>perceptron</td>\n",
       "      <td>elasticnet</td>\n",
       "      <td>0.740485</td>\n",
       "      <td>l1</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>(1, 4)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>26</th>\n",
       "      <td>2.000</td>\n",
       "      <td>False</td>\n",
       "      <td>modified_huber</td>\n",
       "      <td>elasticnet</td>\n",
       "      <td>0.740456</td>\n",
       "      <td>None</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>(1, 4)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0.100</td>\n",
       "      <td>False</td>\n",
       "      <td>modified_huber</td>\n",
       "      <td>l2</td>\n",
       "      <td>0.739598</td>\n",
       "      <td>l1</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>(1, 3)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>0.100</td>\n",
       "      <td>False</td>\n",
       "      <td>perceptron</td>\n",
       "      <td>elasticnet</td>\n",
       "      <td>0.739587</td>\n",
       "      <td>None</td>\n",
       "      <td>True</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>(1, 3)</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    clf__alpha  clf__fit_intercept       clf__loss clf__penalty     score  \\\n",
       "12       1.000                True  modified_huber   elasticnet  0.752386   \n",
       "25       1.000               False  modified_huber   elasticnet  0.750727   \n",
       "16       0.001               False      perceptron           l1  0.744715   \n",
       "29       2.000                True  modified_huber   elasticnet  0.744708   \n",
       "2        0.100               False  modified_huber   elasticnet  0.743958   \n",
       "5        0.010               False      perceptron   elasticnet  0.742129   \n",
       "3        0.001               False      perceptron   elasticnet  0.740485   \n",
       "26       2.000               False  modified_huber   elasticnet  0.740456   \n",
       "1        0.100               False  modified_huber           l2  0.739598   \n",
       "13       0.100               False      perceptron   elasticnet  0.739587   \n",
       "\n",
       "   tfidf__norm  tfidf__smooth_idf  tfidf__sublinear_tf  tfidf__use_idf  \\\n",
       "12        None              False                False            True   \n",
       "25        None              False                False            True   \n",
       "16          l2               True                False            True   \n",
       "29        None               True                False            True   \n",
       "2         None              False                False            True   \n",
       "5           l2               True                 True            True   \n",
       "3           l1               True                False           False   \n",
       "26        None               True                False            True   \n",
       "1           l1               True                False           False   \n",
       "13        None               True                 True           False   \n",
       "\n",
       "   vect__ngram_range  \n",
       "12            (1, 3)  \n",
       "25            (1, 3)  \n",
       "16            (1, 2)  \n",
       "29            (1, 2)  \n",
       "2             (1, 3)  \n",
       "5             (1, 2)  \n",
       "3             (1, 4)  \n",
       "26            (1, 4)  \n",
       "1             (1, 3)  \n",
       "13            (1, 3)  "
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "parameters = {\n",
    "    'vect__ngram_range': [(1, 2), (1, 3), (1, 4)],\n",
    "    'tfidf__norm': (None, 'l2', 'l1'),\n",
    "    'tfidf__use_idf': [True, False],\n",
    "    'tfidf__sublinear_tf': [True, False],\n",
    "    'tfidf__smooth_idf': [True, False],\n",
    "    'clf__penalty': ('l1', 'elasticnet'),\n",
    "    'clf__loss': ('modified_huber', 'perceptron'),\n",
    "    'clf__fit_intercept': [True, False],\n",
    "    'clf__alpha': (2, 1, 1e-1, 1e-2, 1e-3, 1e-4)#\n",
    "}\n",
    "\n",
    "Search(parameters).head(10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {},
   "outputs": [],
   "source": [
    "parameters = {\n",
    "    'vect__ngram_range': [(1, 2), (1, 3), (1, 4)],\n",
    "    'tfidf__norm': (None, 'l2', 'l1'),\n",
    "    'tfidf__use_idf': [True],\n",
    "    'tfidf__sublinear_tf': [False],\n",
    "    'tfidf__smooth_idf': [True, False],\n",
    "    'clf__penalty': ('l1', 'elasticnet'),\n",
    "    'clf__loss': ('modified_huber', 'perceptron'),\n",
    "    'clf__fit_intercept': [True, False],\n",
    "    'clf__alpha': (1.5, 1, 1e-1, 1e-2, 0.001, 0.0005)#\n",
    "}\n",
    "\n",
    "gs_clf =  GridSearchCV(text_clf, parameters, n_jobs=-1, cv=5, iid=False)\n",
    "gs_clf_result = gs_clf.fit(train_data['content'].values, train_data['tag'].values)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.757477630755669\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "{'clf__alpha': 1,\n",
       " 'clf__fit_intercept': True,\n",
       " 'clf__loss': 'modified_huber',\n",
       " 'clf__penalty': 'elasticnet',\n",
       " 'tfidf__norm': None,\n",
       " 'tfidf__smooth_idf': False,\n",
       " 'tfidf__sublinear_tf': False,\n",
       " 'tfidf__use_idf': True,\n",
       " 'vect__ngram_range': (1, 3)}"
      ]
     },
     "execution_count": 54,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "print(gs_clf_result.best_score_)\n",
    "gs_clf_result.best_params_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "metadata": {},
   "outputs": [],
   "source": [
    "parameters = {\n",
    "    'vect__ngram_range': [(1, 2), (1, 3)],\n",
    "    'tfidf__norm': [None, 'l2'],\n",
    "    'tfidf__use_idf': [True],\n",
    "    'tfidf__sublinear_tf': [False],\n",
    "    'tfidf__smooth_idf': [False],\n",
    "    'clf__penalty': ['elasticnet'],\n",
    "    'clf__loss': ['modified_huber'],\n",
    "    'clf__fit_intercept': [True, False],\n",
    "    'clf__alpha': np.linspace(0.8, 1.2, 10)\n",
    "}\n",
    "\n",
    "gs_clf =  GridSearchCV(text_clf, parameters, n_jobs=-1, cv=5, iid=False)\n",
    "gs_clf_result = gs_clf.fit(train_data['content'].values, train_data['tag'].values)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.751523790149827\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "{'clf__alpha': 1.2,\n",
       " 'clf__fit_intercept': False,\n",
       " 'clf__loss': 'modified_huber',\n",
       " 'clf__penalty': 'elasticnet',\n",
       " 'tfidf__norm': None,\n",
       " 'tfidf__smooth_idf': False,\n",
       " 'tfidf__sublinear_tf': False,\n",
       " 'tfidf__use_idf': True,\n",
       " 'vect__ngram_range': (1, 2)}"
      ]
     },
     "execution_count": 56,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "print(gs_clf_result.best_score_)\n",
    "gs_clf_result.best_params_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.7642553191489362"
      ]
     },
     "execution_count": 58,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "predict_tag = gs_clf_result.predict(train_data['content'].values)\n",
    "result = np.array(predict_tag == train_data['tag'].values)\n",
    "result.mean()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 119,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/home/lx/.local/lib/python3.6/site-packages/sklearn/model_selection/_search.py:841: DeprecationWarning: The default of the `iid` parameter will change from True to False in version 0.22 and will be removed in 0.24. This will change numeric results when test-set sizes are unequal.\n",
      "  DeprecationWarning)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.7683578104138852\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "{'clf__alpha': 0.0192,\n",
       " 'clf__fit_intercept': True,\n",
       " 'clf__loss': 'squared_hinge',\n",
       " 'clf__penalty': 'none',\n",
       " 'tfidf__norm': 'l2',\n",
       " 'tfidf__smooth_idf': False,\n",
       " 'tfidf__sublinear_tf': False,\n",
       " 'tfidf__use_idf': True,\n",
       " 'vect__ngram_range': (1, 2),\n",
       " 'vect__tokenizer': <function __main__.tok2(sentence)>}"
      ]
     },
     "execution_count": 119,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def tok1(sentence):\n",
    "    return jieba.analyse.extract_tags(sentence, topK=int(sqrt(len(sentence))))\n",
    "\n",
    "def tok2(sentence):\n",
    "    return jieba.lcut(sentence)\n",
    "\n",
    "\n",
    "text_clf = Pipeline([\n",
    "    ('vect', CountVectorizer()),\n",
    "    ('tfidf', TfidfTransformer()),\n",
    "    ('clf', SGDClassifier(random_state=ord(os.urandom(1)), max_iter=128, tol=1e-3)) # sublinear_tf \n",
    "])\n",
    "\n",
    "\n",
    "parameters = {\n",
    "    'vect__tokenizer': [tok2],\n",
    "    'vect__ngram_range': [(1, 2)],\n",
    "    'tfidf__norm': ['l2'],\n",
    "    'tfidf__use_idf': [True, False],\n",
    "    'tfidf__sublinear_tf': [True, False],\n",
    "    'tfidf__smooth_idf': [True, False],\n",
    "    'clf__penalty': ['none', 'l2'],\n",
    "    'clf__loss': ['squared_hinge', 'perceptron'],\n",
    "    'clf__fit_intercept': [True],\n",
    "    'clf__alpha': np.linspace(0.018, 0.020, 11)\n",
    "}\n",
    "\n",
    "gs_clf = GridSearchCV(text_clf, parameters, n_jobs=-1, cv=5)\n",
    "gs_clf_result = gs_clf.fit(train_data['content'].values, train_data['tag'].values)\n",
    "\n",
    "print(gs_clf_result.best_score_)\n",
    "gs_clf_result.best_params_"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "ds",
   "language": "python",
   "name": "ds"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
